<?php
namespace jy_weishop;
defined('BY_JYA') or exit('error');
/*
	门店
*/
class queue extends \plugin{
	//生成排号
	public function create($params){
		if(!$params['store_id']){
			exi('请选择门店','error');
		}
		if(!$params['num']){
			exi('至少一位大人','error');
		}
		$data=array(
			'num'=>intval($params['num']),
			'childnum'=>intval($params['childnum']),
		);
		$setnumber=$this->setnumber($params);
		$data['uniacid']=$_SESSION['uniacid'];
		$data['mid']=$_SESSION['uid'];
		$data['store_id']=$params['store_id'];
		$data['number']=$setnumber['number'];
		$data['queue_id']=$setnumber['queue_id'];
		$data['createtime']=TIMESTAMP;
		pdo_insert('jy_weishop_queue_member',$data);
		$id=pdo_insertid();
		$this->model('notice')->notice_queuecall($data);
		return $id;
	}
	//获取排号
	public function setnumber($params){
		$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
		$number = pdo_getcolumn('jy_weishop_queue_member',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid'],'store_id'=>$params['store_id'],'status'=>0,'createtime >'=>$daytime),'number');
		if($number){
			exi('您已经取号了！您的排号：'.$number,'error');
		}
		$num=$params['num']+$params['childnum'];
		$queue = pdo_getall('jy_weishop_queue',array('uniacid'=>$_SESSION['uniacid'],'store_id'=>$params['store_id'],'status'=>1,'limit_num >='=>$num));
		paixu($queue,'limit_num','asc');
		if(empty($queue)){
			exi('该门店，没有适合的'.$num.'人排号餐桌！','error');
		}else{
			foreach($queue as $item){
				if($num<=$item['limit_num']){
					$queue_id = $item['id'];
					$queue_pre = $item['pre'];
					break;
				}
			}
		}
		$number = pdo_count('jy_weishop_queue_member',array('uniacid'=>$_SESSION['uniacid'],'store_id'=>$params['store_id'],'queue_id'=>$queue_id,'createtime >'=>$daytime));		
		$number = $queue_pre.sprintf("%05d", $number+1);
		return array('number'=>$number,'queue_id'=>$queue_id);
	}
}